<template>
  <div class="usersmanage-container">
    <breadcrumb style="position: absolute;width:100%;"></breadcrumb>
    <div class="content">
      <div class="query">
        <div class="btn">
          <el-button type="primary" @click="$router.go(-1)">返回</el-button>
        </div>
      </div>
      <div class="table">
        <el-table :data="usersList" style="width: 100%" height="598">
          <el-table-column prop="usrNo" label="用户ID"></el-table-column>
          <el-table-column prop="userName" label="用户昵称"></el-table-column>
          <el-table-column prop="createtime" label="入团时间"></el-table-column>
          <el-table-column label="与团长的团队关系">
            <template slot-scope="scope">
              <span>{{scope.row.inviterStatus}}</span>
              <div id="box">
                <el-button
                  type="text"
                  size="small"
                  v-if="scope.row.inviterStatus=='解除'"
                  @click="handle_(scope.row.inviterStatus,scope.row.userAssetVersion,scope.row.userId)"
                >存续</el-button>
                <el-button
                  type="text"
                  size="small"
                  v-else
                  @click="handleForbidden(scope.row.inviterStatus,scope.row.userId,scope.row.userAssetVersion)"
                >解除</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          :currentPage="currentPage"
          :page="page"
          :total="total"
          :pageSize="pageSize"
          @change-current="currentChange"
          @change-size="sizeChange"
        ></pagination>
        <el-dialog :visible.sync="dialogVisible" width="16%">
          <el-select v-model="restoreWay " style="width:240px">
            <el-option
              v-for="item in options1"
              :key="item.value"
              :label="item.label"
              :value="item.id"
            ></el-option>
          </el-select>
          <div slot="footer" class="dialog-footer">
            <div class="btn">
              <el-button @click="dialogVisible=false" size="small" style="width:120px">取 消</el-button>
              <el-button type="primary" @click="handleSuccess" size="small" style="width:120px">确 定</el-button>
            </div>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import breadcrumb from "../../components/breadcrumb";
import pagination from "../../components/page";

export default {
  components: { breadcrumb, pagination },
  data() {
    return {
      id: this.$route.query.id,
      currentPage: 1,
      pageSize: 10,
      usersList: [],
      options1: [
        { id: "RestoreFull", label: "解除期间佣金计入——存续" },
        { id: "RestoreGeneral", label: "解除期间佣金不计入——存续" }
      ],
      dialogVisible: false,
      total: 0,
      value: "",
      value1: "",
      value2: "",
      value3: "",
      restoreWay: "",
      invitStatus: "",
      userAssetVersion: "",
      usrId: "",
      page: 1
    };
  },
  created() {
    if (localStorage.getItem("pageMsg")) {
      let pageMsg = JSON.parse(localStorage.getItem("pageMsg"));
      this.currentPage = pageMsg.currentPage;
      this.pageSize = pageMsg.pageSize;
      localStorage.removeItem("pageMsg");
    }
    this.get_usersList();
  },
  methods: {
    sizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.get_usersList();
    },
    currentChange(val) {
      this.currentPage = val;
      this.get_usersList();
    },
    get_usersList() {
      this.usersList = [];
      var data =
        "current=" +
        this.currentPage +
        "&size=" +
        this.pageSize +
        "&inviterAmount=" +
        this.id;
      this.$axios
        .post("/web_userteam/select_direct_info", data)
        .then(result => {
          if (result.data.status == 0) {
            this.total = result.data.data.total;
            this.page = Math.ceil(result.data.data.total / this.pageSize);
            this.usersList = result.data.data.records;
            this.usersList.forEach(item => {
              if (item.userId) {
                item.inviterStatus = item.inviterStatus == 1 ? "存续" : "解除";
              }
            });
          }
        });
    },
    handleForbidden(inviterStatus, userId, userAssetVersion) {
      var status;
      if (inviterStatus == "存续") {
        status = 0;
      } else {
        status = 1;
      }
      let data =
        "usrId=" +
        userId +
        "&invitStatus=" +
        status +
        "&userAssetVersion=" +
        userAssetVersion;
      this.$confirm("此操作将修改状态, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$axios
            .post("/web_userteam/update_inviter", data)
            .then(result => {
              if (result.data.status == 0) {
                if (result.data.data == true) {
                  if (inviterStatus == "存续") {
                    this.get_usersList();
                    return this.$message({
                      type: "success",
                      message: "解除成功"
                      //message:'冻结成功'
                    });
                  } else {
                    this.get_usersList();
                    return this.$message({
                      type: "success",
                      message: "存续成功"
                      //message:'启用成功'
                    });
                  }
                } else {
                  return this.$message({
                    type: "warning",
                    message: "修改失败"
                  });
                }
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消修改"
          });
        });
    },
    handle_(invitStatus, userAssetVersion, usrId) {
      this.dialogVisible = true;
      localStorage.setItem(
        "pageMsg",
        JSON.stringify({
          invitStatus,
          userAssetVersion,
          usrId
        })
      );
    },
    handleSuccess() {
      if (this.restoreWay == "") {
        return this.$message({
          type: "warning",
          message: "佣金存续状态不能为空"
        });
      }
      if (localStorage.getItem("pageMsg")) {
        let pageMsg = JSON.parse(localStorage.getItem("pageMsg"));
        this.invitStatus = pageMsg.invitStatus;
        this.userAssetVersion = pageMsg.userAssetVersion;
        this.usrId = pageMsg.usrId;
        localStorage.removeItem("pageMsg");
      }
      var status;
      if (this.inviterStatus == "存续") {
        status = 0;
      } else {
        status = 1;
      }
      let data =
        "invitStatus=" +
        status +
        "&restoreWay=" +
        this.restoreWay +
        "&userAssetVersion=" +
        this.userAssetVersion +
        "&usrId=" +
        this.usrId;

      this.$axios.post("/web_userteam/update_inviter", data).then(result => {
        if (result.data.status == 0) {
          if (result.data.data == true) {
            if (this.inviterStatus == "存续") {
              this.get_usersList();
              this.dialogVisible = false;
              return this.$message({
                type: "success",
                message: "解除成功"
              });
            } else {
              this.get_usersList();
              this.dialogVisible = false;
              return this.$message({
                type: "success",
                message: "存续成功"
              });
            }
          } else {
            this.dialogVisible = false;
            return this.$message({
              type: "warning",
              message: "修改失败"
            });
          }
        }
      });
    }
  }
};
</script>


<style lang="less" >
.usersmanage-container {
  @import "../../style/table.less";
  @import "../../style/content.less";
  display: flex;
  flex-flow: column;
  position: relative;
  height: 100%;
  .el-table .cell {
    white-space: pre-line !important;
  }
  #box {
    display: none;
  }
  .el-table__body tr:hover {
    #box {
      display: block;
    }
  }
}
</style>
